<template>
	<view class="attestation">
		<view class="id-info">
			<view class="verified-header-item">
				<text>用户姓名：</text>
				<input placeholder="请填写" v-model="name" />
			</view>
			<view class="verified-header-item">
				<text>身份证号：</text>
				<input type="idcard" placeholder="请填写" v-model="idCard" />
			</view>
			<text class="title">{{isVerified?'身份证信息':'上传身份证信息'}}</text>
			<view class="positive" @click="addPhoto('positive')">
				<image :src="idCardPositive" mode=""></image>
				<text> {{isVerified?'身份证正面人像图片':'点击上传身份证正面人像图片'}}</text>
			</view>
			<view class="negative" @click="addPhoto('negative')">
				<image :src="idCardNegative" mode=""></image>
				<text>{{isVerified?'身份证反面国徽图片':'点击上传身份证反面国徽图片'}}</text>
			</view>
			<view class="prompt" v-if="!isVerified">
				<text class="prompt-title">拍摄时，确认身份边框完整、字体清晰、亮度均匀</text>
			</view>
			<radio-group>
				<view v-for="(item, index) in List" :key="index" class="agreement">
						<radio :class="active == index ? 'checked' : ''" :checked="active== index" :value="index" @click="radioChange(index)"/>
						<view>同意<text @click="gotoagreement">《用户服务协议》</text>和<text @click="gotoprivacypolicy">《隐私政策》</text></view>
				</view>
			</radio-group>
		</view>
		<button class="submit" v-if="!isVerified" @click="submit">提交审核</button>
	</view>
</template>

<script>
	import api from '@/static/js/api.js'

	import {
		pathToBase64,
		base64ToPath
	} from '@/static/js/photoUtils.js'
	import {
		verified
	} from '@/static/js/api/user.js'
	import request from '../../../static/js/request.js'

	export default {
		data() {
			return {
				isVerified: false,
				//正面
				idCardPositive: require('@/static/image/mine/idcard2.png'),
				// 反面
				idCardNegative: require('@/static/image/mine/idcard1.png'),
				account_type: '',
				choosePhoto1: false,
				choosePhoto2: false,
				chooseDatas: false,
				companyName: '',
				socialCode: '',
				datas: require('@/static/image/mine/yyzz.png'), //营业执照
				idCard: '',
				name: '',
				List: [{
						id: '1'
				}],
				active: null // 选中的序号
			}
		},
		methods: {
			radioChange(index) {
				if (this.active== index) { // 如果已经选中，则取消选中
						this.active= null;
				} else { // 否则进行选中赋值
						this.active= index;
				}
			},
			gotoagreement(){
				uni.navigateTo({
					url: '/pagesB/agreement/agreement'
				})
			},
			gotoprivacypolicy(){
				uni.navigateTo({
					url: '/pagesB/agreement/privacyPolicy'
				})
			},
			IDCard(value) {
				let reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
				return reg.test(value)
			},
			addPhoto(type) {
				let self = this;
				if (!this.isVerified) {
					uni.chooseImage({
						count: 1,
						success(res) {
							uni.compressImage({
								src: res.tempFilePaths[0],
								quality: 50,
								success: res => {
									console.log(res)
									if (type == "positive") {
										self.idCardPositive = res.tempFilePath;
										console.log(self.idCardPositive)
										self.choosePhoto1 = true;
									} else if (type == "negative") {
										self.idCardNegative = res.tempFilePath;
										self.choosePhoto2 = true;
									} else if (type == "datas") {
										self.datas = res.tempFilePath;
										self.chooseDatas = true;
									}
								}
							})
						}
					})
				}
			},
			async submit() {
				uni.showLoading({
					title: '正在加载'
				})
				let res1 = '';
				let res2 = '';
				let res3 = '';
				if (this.$u.trim(this.name, 'left') == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入用户姓名'
					})
					return
				}
				if (!this.IDCard(this.idCard)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的身份证号码'
					})
					return
				}
				if (!this.choosePhoto1) {
					uni.showToast({
						icon: 'none',
						title: '请上传身份证正面人像图片'
					})
					return
				}
				if (!this.choosePhoto2) {
					uni.showToast({
						icon: 'none',
						title: '请上传身份证反面国徽图片'
					})
					return
				}
				if (this.active!=0) {
					uni.showToast({
						icon: 'none',
						title: '请阅读《用户服务协议》和《隐私政策》'
					})
					return
				}
				if (this.choosePhoto1) {
					res1 = await pathToBase64(this.idCardPositive);
					// console.log(res1,3333)
					// res1 = this.idCardPositive;
				}
				if (this.choosePhoto2) {
					res2 = await pathToBase64(this.idCardNegative);
					// res2 = this.idCardNegative;
				}
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				// var user_type = userInfo.account_type;
				var user_type = '工人';
				var idcard_file_a = res1 ? res1.replace('data:image/png;base64,', '') : '';
				var idcard_file_b = res2 ? res2.replace('data:image/png;base64,', '') : '';
				api.realNameAuthentication(userInfo.access_token, 'res.partner', '_api_upload_user_id', userInfo.uid,
					this.$u.trim(this.name, 'left'), idcard_file_a, idcard_file_b, user_type, this.idCard).then(
					resp => {
						userInfo.is_confirm = resp.is_confirm
						uni.setStorageSync('userInfo', JSON.stringify(userInfo));
						uni.showToast({
							title: '认证成功'
						})
						setTimeout(() => {
							// uni.switchTab({
							// 	url: '/pages/main/mine'
							// })
							uni.navigateBack();
						}, 1000)
					})
			},
		}
	}
</script>

<style lang="less" scoped>
	.id-info {
		display: flex;
		flex-direction: column;
		background: #fff;
		padding: 32rpx 34rpx;

		.positive {
			display: flex;
			flex-direction: column;
			margin-top: 34rpx;
			align-items: center;

			.yyzz {
				width: 484rpx;
				height: 484rpx;
			}

			image {
				width: 484rpx;
				height: 302rpx;
			}

			text {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
			}
		}

		.negative {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 34rpx;

			image {

				width: 484rpx;
				height: 302rpx;
			}

			text {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
			}
		}

		.prompt {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 48rpx;

			.prompt-title {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				margin-bottom: 12rpx;
			}

			.prompt-img {
				display: flex;
				align-items: center;

				.prompt-img-item:last-child {
					margin-right: 0rpx;
				}

				.prompt-img-item {
					// flex: 1;
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-right: 48rpx;

					image {
						width: 126rpx;
						height: 116rpx;

					}

					text {
						font-weight: 400;
						font-size: 26rpx;
						color: #A4A4A6;
					}
				}
			}
		}
		.agreement{
			display: flex;
			align-items: center;
			margin-top: 20rpx;
			text{
				color: #0091FF;
			}
		}
	}

	.submit {
		font-weight: 500;
		font-size: 32rpx;
		height: 80rpx;
		background: #B8212B;
		border-radius: 8rpx;
		display: flex;
		color: #FFFFFF;
		align-items: center;
		justify-content: center;
		margin: 40rpx 34rpx 40rpx;
	}

	.verified-header-item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;

		input {
			flex: 1;
			height: 44rpx;
			background: #EFF0F1;
			border-radius: 8rpx;
			padding: 14rpx 20rpx;
		}
	}
</style>
